
var db = require('../db/db.js');
var config = require('../config.js');

var goods_spec_model = require("./yoshop_goods_spec");

var model = {
    tablename: "yoshop_goods",
    add: async function (obj) {

        var sql = "insert into  " + this.tablename + "(goods_name,category_id,spec_type,deduct_stock_type,content,sales_initial,sales_actual,goods_sort,delivery_id,goods_status,is_delete,create_time,typeid,brandid,wxapp_id,limits,tags)"
            + "  values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        var results = await db.execute(sql, [obj.goods_name, obj.category_id, obj.spec_type, obj.deduct_stock_type, obj.content, obj.sales_initial, obj.sales_actual,
        obj.goods_sort, obj.delivery_id, obj.goods_status, obj.is_delete, obj.create_time, obj.typeid, obj.brandid, obj.wxapp_id, obj.limits, obj.tags
        ]);
        console.log("results", results);
        return results;

    },
    edit: async function (obj) {
        var sql = "update  " + this.tablename + " set goods_name=?,category_id=?,spec_type=?,deduct_stock_type=?,content=?,sales_initial=?,sales_actual=?,goods_sort=?,delivery_id=?"
            + ",goods_status=?,is_delete=?,update_time=?,typeid=?,recommendnum=?,shardnum=?,brandid=?,limits=?,tags=? where id=?";
        var results = await db.execute(sql, [obj.goods_name, obj.category_id, obj.spec_type, obj.deduct_stock_type, obj.content, obj.sales_initial, obj.sales_actual,
        obj.goods_sort, obj.delivery_id, obj.goods_status, obj.is_delete, obj.update_time, obj.typeid, obj.recommendnum, obj.shardnum, obj.brandid, obj.limits, obj.tags, obj.id]);
        console.log("results", results);
        return results;

    },
    del: async function (where) {
        var sql = "delete from  " + this.tablename + " where " + where;
        var results = await db.execute(sql, []);
        return results;

    },
    getlist: async function (rows, first, where, join = "", order = " order by  id desc", field = "*", group = " ") {
        var sql = "select " + field + "  from  " + this.tablename + " " + join + "  where " + where + " " + group + " " + order + " limit " + rows + " offset  " + first;
        console.log("sql", sql);
        var results = await db.query(sql, []);
        return results;

    },
    getcount: async function (where, join = "", group = "") {
        var countinfo = await db.query("select count(*) as num  from " + this.tablename + " " + join + "   where  " + where + group, []);
        return countinfo[0].num;
    },
    summoney: async function (where, key) {
        var countinfo = await db.query("select  sum(" + key + ") as num  from " + this.tablename + "  where  " + where, []);
        return countinfo[0].num;
    },
    getinfo: async function (where, join = "", order = "order by id desc") {
        var sql = "select *  from  " + this.tablename + join + " where " + where + " " + order;
        var results = await db.query(sql, []);
        return results;

    },
    getbody: async function (obj, id) {
        var good = await this.getinfo("id=" + id);
        if (good.length > 0) {
            obj.goods_name = good[0].goods_name;
        }

        var goods_spec = await goods_spec_model.getinfo("goods_id=" + id);
        if (goods_spec.length > 0) {
            obj.goods_price = goods_spec[0].goods_price;
            obj.line_price = goods_spec[0].line_price;
            obj.stock_num = goods_spec[0].stock_num;
            obj.good_weight = goods_spec[0].good_weight;
            obj.spec_value = goods_spec[0].spec_value;

        }

        var goodlist = await db.query("select yoshop_upload_file.file_name from yoshop_goods_image join yoshop_upload_file on yoshop_upload_file.id=yoshop_goods_image.image_id  where yoshop_goods_image.goods_id=" + id);
        if (goodlist.length > 0) {
            obj.imgurl = config.url + goodlist[0].file_name;
        }
    }


}


module.exports = model;